package com.longdaji.decoration.ui.activitiesOfMine.pendingEvaluated;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.PendingEvaluatedOrderAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.ui.activitiesOfCommunity.publishDynamic.PublishDynamicContract;
import com.longdaji.decoration.ui.activitiesOfMine.pendingReceiving.SeeLogisticsActivity;
import com.longdaji.decoration.ui.activitiesOfMine.refund.forRefund.ForRefundActivity;
import com.longdaji.decoration.ui.activitiesOfMine.refund.refundDetail.RefundDetailActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PendingEvaluatedActivity extends BaseActivity implements PendingEvaluatedContract.View {

    @Inject
    PendingEvaluatedContract.Presenter mPresenter;

    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.rv_pending_evaluated)
    RecyclerView rvPendingEvaluated;
    @BindView(R.id.sr_pending_evaluate)
    SwipeRefreshLayout srPendingEvaluate;

    private PendingEvaluatedOrderAdapter mAdapter;
    private List<Orders.OrdersBean.OrderBean> mOrderList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pending_evaluated);
        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("已完成");


        initView();
    }

    private void initView() {
        rvPendingEvaluated.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PendingEvaluatedOrderAdapter(mOrderList);

        mAdapter.setEvaluateListener(new PendingEvaluatedOrderAdapter.EvaluateListener() {
            @Override
            public void onEvaluateClick(int position) {
                // TODO: 2018/4/9 0009 go evaluate activity
                Intent intent = new Intent(PendingEvaluatedActivity.this, OrderDetailActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }
        });
        mAdapter.setSeeLogisticsListener(new PendingEvaluatedOrderAdapter.SeeLogisticsListener() {
            @Override
            public void onSeeLogisticsClick(int position) {
                Intent intent = new Intent(PendingEvaluatedActivity.this, SeeLogisticsActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }
        });
        mAdapter.setReturnGoodsListener(new PendingEvaluatedOrderAdapter.ReturnGoodsListener() {
            @Override
            public void onReturnGoodsClick(int position) {
                Intent intent = new Intent(PendingEvaluatedActivity.this, OrderDetailActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }
        });
        rvPendingEvaluated.setAdapter(mAdapter);
        srPendingEvaluate.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        srPendingEvaluate.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });
        mPresenter.initData();
    }

    private void refreshData() {
        mPresenter.initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showPendingEvaluatedOrders(List<Orders.OrdersBean.OrderBean> pendingEvaluatedOrderList) {
        Collections.reverse(pendingEvaluatedOrderList);
        if (pendingEvaluatedOrderList.size() > 0) {
            mAdapter.setData(pendingEvaluatedOrderList);
        } else {
            Toast.makeText(this, "无已完成订单", Toast.LENGTH_SHORT).show();
            mAdapter.clearData();
        }
        if (srPendingEvaluate != null && srPendingEvaluate.isRefreshing()) {
            srPendingEvaluate.setRefreshing(false);
        }
    }
}
